package com.tianpengtech.common;
//lalllllllllllllllllllllll222222222222222333333333
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.cron4j.Cron4jPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.render.ViewType;
import com.jfinal.template.Engine;
import com.tianpengtech.interceptor.GlobalInterceptor;
import com.tianpengtech.interceptor.InjectIntorceptor;
import com.tianpengtech.model.Blog;
import com.tianpengtech.route.AdminRoute;
import com.tianpengtech.route.FrontRoute;
import com.tianpengtech.service.ShareMethod;
import com.tianpengtech.service.ShareObject;

public class MyConfig extends JFinalConfig {

	@Override
	public void configConstant(Constants me) {
		// TODO Auto-generated method stub
		me.setDevMode(true);//开发模式
		// 加载少量必要配置，随后可用PropKit.get(...)获取值
		PropKit.use("a_little_config.txt");
	}
 
	@Override
	public void configRoute(Routes me) {
		// TODO Auto-generated method stub
//		me.add("/", IndexController.class);
//		me.add("/", IndexController.class,"/abc");
//		me.add("/blog", IndexController.class,"/blog");
//		me.add("/user", IndexController.class,"/user");
		me.add(new FrontRoute());
		me.add(new AdminRoute());
	}

	@Override
	public void configEngine(Engine me) {
		// TODO Auto-generated method stub
		me.setBaseTemplatePath(PathKit.getWebRootPath());
		me.addSharedFunction("/front/common.html");
		me.addSharedStaticMethod(ShareObject.class);
		//me.addSharedMethod(new ShareMethod());
		me.addSharedObject("sm", new ShareMethod());
	}

	@Override
	public void configPlugin(Plugins me) {
		// TODO Auto-generated method stub
		// 配置C3p0数据库连接池插件
				DruidPlugin druidPlugin = new DruidPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password").trim());
				me.add(druidPlugin);
				// 配置ActiveRecord插件
				ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
				arp.setBaseSqlTemplatePath(PathKit.getWebRootPath()+"/WEB-INF");//sql在哪个位置
				arp.addSqlTemplate("/sql/demo.sql");
				arp.addMapping("t_blog",Blog.class);
				me.add(arp);
				
				//me.add(new EhCachePlugin());
				
				Cron4jPlugin cron=new Cron4jPlugin(PropKit.use("cron4j.txt"),"cron4j"); 
				me.add(cron);

	}

	@Override
	public void configInterceptor(Interceptors me) {
		// TODO Auto-generated method stub
		me.add(new GlobalInterceptor());//全局拦截
		//me.addGlobalServiceInterceptor(new InjectIntorceptor());
	}

	@Override
	public void configHandler(Handlers me) {
		// TODO Auto-generated method stub
		me.add(new ContextPathHandler("ctx"));
	}
	@Override
	public void afterJFinalStart(){
		System.out.print("jfinal启动了！");
	}
	
	@Override
	public void beforeJFinalStop() {
		// TODO Auto-generated method stub
		System.out.print("jfinal马上停止！");
	}

	public static void main(String[] args) {
		/**
		 * 特别注意：Eclipse 之下建议的启动方式
		 */
		JFinal.start("src/main/webapp", 8089, "/", 5);
		
		/**
		 * 特别注意：IDEA 之下建议的启动方式，仅比 eclipse 之下少了最后一个参数
		 */
		// JFinal.start("src/main/webapp", 80, "/");
	}

}
